package vesion2.tank;

import com.fasterxml.jackson.annotation.JsonIgnore;
import vesion2.main.ObjectManager;
import vesion2.object.Point;
import vesion2.object.Rectangle;
import vesion2.object.special.Orientation;

import javax.swing.text.AbstractDocument;

public class EnemyTank extends BaseTank{
    @JsonIgnore
    private EnemyTankThread thread;

    public EnemyTank(){
        this(new Point());
    }
    public EnemyTank(Point left_up) {
       this(left_up,1);
    }

    public EnemyTank(Point left_up, int ort) {
        super(left_up, ort);
        thread=new  EnemyTankThread();
    }

    public void start(){
        thread.start();
    }

    public void reStart(){
        if(thread.isAlive())
            return;
        thread=new EnemyTankThread();
        thread.start();
    }
    @Override
    public EnemyTank setManager(ObjectManager manager) {
        return (EnemyTank) super.setManager(manager);
    }

    @Override
    public void setAlive(boolean alive) {
        super.setAlive(alive);
        if(!alive)
            manager.incKNum();
    }

    public boolean hasEnemy(){
        Rectangle rectangle=new Rectangle(new Point(),0,0);
        switch (getOrt()){
            case Orientation.UP:
                rectangle.getLeft_up().setX(getLeft_up().getX());
                rectangle.getLeft_up().setY(0);
                rectangle.setWidth(40);
                rectangle.setHeight(getLeft_up().getY());
                break;
            case Orientation.DOWN:
                rectangle.getLeft_up().setX(getLeft_up().getX());
                rectangle.getLeft_up().setY(getLeft_up().getY()+50);
                rectangle.setWidth(40);
                rectangle.setHeight(getManager().getGamePanel().getHeight()- getLeft_up().getY());
                break;
            case Orientation.LEFT:
                rectangle.getLeft_up().setX(0);
                rectangle.getLeft_up().setY(getLeft_up().getY());
                rectangle.setWidth(getLeft_up().getX());
                rectangle.setHeight(40);
                break;
            case Orientation.RIGHT:
                rectangle.getLeft_up().setX(getLeft_up().getX()+50);
                rectangle.getLeft_up().setY(getLeft_up().getY());
                rectangle.setWidth(getManager().getGamePanel().getWidth()-getLeft_up().getX());
                rectangle.setHeight(40);
                break;
            default:break;
        }

        if(getManager().getMyTank().isIntersect(rectangle))
            return true;
        return false;
    }

    @Override
    public String toString() {
        return "EnemyTank{" +
                "isAlive=" + isAlive +
                ", backColor=" + backColor +
                ", left_up=" + left_up +
                ", width=" + width +
                ", height=" + height +
                ", ort=" + ort +
                ", speed=" + speed +
                '}';
    }

    class EnemyTankThread extends Thread {
        public EnemyTankThread() {
            super();
            setName("enemytank");
        }

        @Override
        public void run() {
            super.run();
            int oi=getOrt(),j;
            while (isAlive&&!manager.getGamePanel().isStop()) {
               setOrt(oi);
                for (int i=0;i<30;i++) {
                    if(manager.getGamePanel().isStop())
                        break;
                    j=(int) (Math.random() * 20);
                    if(!move())
                        break;
                    if(j==0||hasEnemy())
                        shitBullet();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                oi = (int) (Math.random() * 7+1)%4;
            }
        }
    }
}
